package com.ycode.bang.anbang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by maosongsong on 2016/4/5.
 */
public class PersonalCenterActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.Lin_change_password)
    LinearLayout mlinChangePassword;
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.btn_quit)
    TextView mBtnQuit;
    @InjectView(R.id.Lin_about_us)
    LinearLayout mlinAboutUs;

    @InjectView(R.id.pingfeng)
    RatingBar mRating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);
        ButterKnife.inject(this);
        initView();

    }

    private void initView() {
//        ToolBarUtils.show(this, mToolbar, true, "找回密码");
        title.setText("个人中心");
        mlinChangePassword.setOnClickListener(this);
        mBtnQuit.setOnClickListener(this);
        mlinAboutUs.setOnClickListener(this);
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Lin_change_password:
                Intent intent1 = new Intent(this, FindPasswordActivity.class);
                startActivity(intent1);
                break;
            case R.id.Lin_about_us:
                Intent intent2 = new Intent(this, AboutUs.class);
                startActivity(intent2);
                break;
            case R.id.btn_quit:
                logout();
                break;
        }
    }

    /**
     * 退出登录
     */
    private void logout() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        FinalHttpUtil.post(RequestMapDev.Logout, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        MyApp.instance.homepageActivity.finish();
                        Intent intent3 = new Intent(PersonalCenterActivity.this, LoginActivity2.class);
                        startActivity(intent3);
                        finish();
                    } else {
                        ToastUtil.show(PersonalCenterActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(PersonalCenterActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(PersonalCenterActivity.this, strMsg);
            }
        });
    }
}
